﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Gecko.DOM;
using System.IO;
using WIKID.Accessor;
using WIKID.Controller;
using WIKID.Model;

namespace WIKID
{
    public partial class frmMain : Form
    {
        // Present the current instance of frmMain
        public static frmMain current;

        private String indexFileAbsolutePath = Directory.GetCurrentDirectory() + @"\Knowledge Sources\1_Knowledge Source 1\Main\index.html";
        //private String questionFileAbsolutePath = Directory.GetCurrentDirectory() + @"\Knowledge Sources\1_Knowledge Source 1\1_Lecture_1\question\rainbow.xml";
        private String questionFileAbsolutePath;
        private String oldQuestionFileAbsolutePath;
        private List<TestView> testViews;
        public AccountModel accountModel;

        public frmMain()
        {
            InitializeComponent();
            current = this;
            btnFinish.Enabled = false;
            accountModel = new AccountModel();
        }

        private void geckoWebBrowser1_DomMouseUp(object sender, Gecko.GeckoDomMouseEventArgs e)
        {
            if (geckoWebBrowser1.Document.GetElementById("data").DomObject != null)
            {
                GeckoInputElement data = new GeckoInputElement(geckoWebBrowser1.Document.GetElementById("data").DomObject);
                if (data.Value == "exit")
                {
                    oldQuestionFileAbsolutePath = data.Value;
                    ResetComponents();
                    return;
                }

                if (!data.Value.Contains('\\'))
                {
                    return;
                }

                questionFileAbsolutePath = Directory.GetCurrentDirectory() + data.Value;

                if (questionFileAbsolutePath.Equals(oldQuestionFileAbsolutePath))
                {
                    return;
                }

                oldQuestionFileAbsolutePath = questionFileAbsolutePath;

                if (!File.Exists(questionFileAbsolutePath))
                {
                    MessageBox.Show("Can not find the Question file: \n" + questionFileAbsolutePath);
                    return;
                }

                this.splitContainer1.SplitterDistance = this.Width * 3 / 4;
                
                testViews = new List<TestView>();

                List<QuestionModel> questionModels = new XMLReader().getQuestionsByXmlFileName(questionFileAbsolutePath);
                var questionController = new QuestionController();
                questionModels.Sort(questionController.CompareQuestionByID);
                int x = this.splitContainer1.SplitterDistance;
                
                int y = 0;
                foreach (QuestionModel questionModel in questionModels)
                {
                    // 
                    // Create a new TestView
                    // t6
                    TestView testView = new TestView();
                    //testView.Location = new System.Drawing.Point(x, y);
                    testView.AutoSize = true;
                    testView.Dock = System.Windows.Forms.DockStyle.Top;
                    testView.Location = new System.Drawing.Point(x, y);
                    testView.Name = "testView" + "Question" + questionModel.ID;
                    testView.AutoSize = true;
                    testView.TabIndex = 1;
                    testView.Padding = new Padding(0, 10, 0, 10);
                    testView.questionModel = questionModel;
                    //add into List View Question
                    splitContainer1.Panel2.Controls.Add(testView);
                    y += testView.Height;

                    //add to List<TestView> testViews
                    testViews.Add(testView);
                }
                
                splitContainer1.Panel2.Enabled = true;
                this.splitContainer1.Panel2.Show();
                splitContainer1.Panel2.Refresh();
            }
        }

        private void frmMain_Load(object sender, EventArgs e)
        {
            testViews = new List<TestView>();
            
            if (File.Exists(indexFileAbsolutePath))
            {
                geckoWebBrowser1.Navigate(indexFileAbsolutePath);
            }
            else
            {
                MessageBox.Show("Can not find the Index file: \n" + indexFileAbsolutePath );
                return;
            }

            ResetComponents();
        }

        private void btnFinish_Click(object sender, EventArgs e)
        {

            //if (MessageBox.Show("Are you sure ?", "WIKID says:", MessageBoxButtons.YesNo) == DialogResult.No)
            //    return;

            int rightAnswerCount = 0;
            foreach (TestView testView in testViews)
            {
                rightAnswerCount += testView.iPoint;
            }

            ResetComponents();

            String text = "Your result: " + rightAnswerCount + "/" + testViews.Count;

            decimal decScore = rightAnswerCount * 10 / testViews.Count;            

            String lectureFileAbsolutePath = Directory.GetCurrentDirectory() + @"\Knowledge Sources\1_Knowledge Source 1\Lectures.xml";
            LectureController lectureController = new LectureController(lectureFileAbsolutePath);
            
            ScoreModel scoreModel = new ScoreModel();
            scoreModel.Date = DateTime.Now;
            scoreModel.LectureId = lectureController.getLectureIDbyLectureFilePath(questionFileAbsolutePath).ToString();
            scoreModel.StudyingScore = decScore;

            String txtMessage = getMessageByScore(decScore);
            String txtTitle = "Wikid Says:";
            String txtResult = rightAnswerCount + "/" + testViews.Count;
            String txtScore = decScore.ToString();
            Image image = getEmoticonByScore(decScore);
            ResultMessageBox.Show(txtMessage, txtTitle, txtResult, txtScore,image);

            rightAnswerCount = 0;
            var accountController = new AccountController();
            accountController.UpdateScore(accountModel, scoreModel);
        }

        private static String getMessageByScore(decimal decScore)
        {
            String text="Congratulate!";
            if (decScore < 5)
            {
                text = "Life is about making mistakes and learning from them.";
            }
            else if (decScore >= 5 && decScore < 8)
            {
                text = "The best preparation for tomorrow is doing your best today!";
            }
            else if (decScore >= 8)
            {
                text = "Kiss your Brain, Kid!";
            }
            return text;
        }

        private static Image getEmoticonByScore(decimal decScore)
        {
            Image image = new Bitmap(global::WIKID.Properties.Resources.love);
            if (decScore < 5)
            {
                image = global::WIKID.Properties.Resources.cry;
            }
            else if (decScore >= 5 && decScore < 8)
            {
                image = global::WIKID.Properties.Resources.good;
            }
            else if (decScore >= 8)
            {
                image = global::WIKID.Properties.Resources.love;
            }
            return image;
        }

        private void ResetComponents()
        {
            foreach(TestView testView in testViews)
            {
                splitContainer1.Panel2.Controls.Remove(testView);
            }

            cbxFinish.Checked = false;

            splitContainer1.Panel2.Enabled = false;
            splitContainer1.Panel2.Hide();
            splitContainer1.SplitterDistance = this.Width;
        }

        private void cbxFinish_CheckedChanged(object sender, EventArgs e)
        {
            if (cbxFinish.Checked)
            {
                btnFinish.Enabled = true;
            }
            else btnFinish.Enabled = false;
        }

        private void frmMain_FormClosed(object sender, FormClosedEventArgs e)
        {
            frmLogin.current = new frmLogin();
            frmLogin.current.Show();
        }

    }
}
